package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.TipsBean;
import com.attendance.www.hr.dao.mapper.EmployeeRoleMapper;
import com.attendance.www.hr.dao.mapper.EmployeeSchedulMapper;
import com.attendance.www.hr.dao.mapper.SchedulDayMapper;
import com.attendance.www.hr.dao.mapper.TipsExtMapper;
import com.attendance.www.hr.entitys.EmployeeRole;
import com.attendance.www.hr.entitys.EmployeeRoleExample;
import com.attendance.www.hr.entitys.EmployeeSchedul;
import com.attendance.www.hr.entitys.EmployeeSchedulExample;
import com.attendance.www.hr.entitys.SchedulDayExample;
import com.attendance.www.hr.entitys.SchedulDayKey;
import com.attendance.www.hr.service.ITipsService;

@Service
public class TipsServiceImpl implements ITipsService {

	@Resource
	private TipsExtMapper tipsDao;
	@Resource
	private EmployeeRoleMapper employeeRoleDao;
	@Resource
	private EmployeeSchedulMapper employeeSchedulDao;
	@Resource
	private SchedulDayMapper schedulDayMapper;
	
	/**
	 * 获取待审批个数信息--zx
	 */
	@Override
	public TipsBean getTips(Integer employeeId) {
		EmployeeRoleExample erExample=new EmployeeRoleExample();
		erExample.or().andEmployeeIdEqualTo(employeeId)
		.andFromDateLessThanOrEqualTo(new Date())
		.andThruDateGreaterThanOrEqualTo(new Date());
		erExample.or().andEmployeeIdEqualTo(employeeId)
		.andFromDateLessThanOrEqualTo(new Date())
		.andThruDateIsNull();
		
		List<EmployeeRole> erList=employeeRoleDao.selectByExample(erExample);
		List<Integer> roleIdList=new ArrayList<Integer>();
		if (erList.size()>0) {
			for (EmployeeRole employeeRole : erList) {
				roleIdList.add(employeeRole.getRoleId());
			}
		}
		
		TipsBean tipsBean=new TipsBean();
		tipsBean.setBusinessTravelNum(tipsDao.getBusinessTravelNum(roleIdList));
		tipsBean.setFurloughNum(tipsDao.getFurloughNum(roleIdList));
		tipsBean.setGoOutNum(tipsDao.getGoOutNum(roleIdList));
		tipsBean.setOverTimeNum(tipsDao.getOverTimeNum(roleIdList));
		tipsBean.setRetroactiveNum(tipsDao.getRetroactiveNum(roleIdList));
		Integer total=tipsBean.getBusinessTravelNum()+tipsBean.getFurloughNum()+tipsBean.getGoOutNum()+tipsBean.getOverTimeNum()+tipsBean.getRetroactiveNum();
		tipsBean.setTotal(total);
		return tipsBean;
	}

	/**
	 * 判断是否是休息日
	 */
	@Override
	public TipsBean getAttendanceTime(Date attendanceDate, Integer employeeId) {
		Integer isRestDay=1;//1是休息日，0是工作日
		
		EmployeeSchedulExample employeeSchedulExample=new EmployeeSchedulExample();
		employeeSchedulExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andFromDateLessThanOrEqualTo(attendanceDate).andThruDateGreaterThanOrEqualTo(attendanceDate);
		employeeSchedulExample.setOrderByClause("sw_id desc");
		List<EmployeeSchedul> esList=employeeSchedulDao.selectByExample(employeeSchedulExample);
		if (esList.size()>0) {
			EmployeeSchedul employeeSchedul=esList.get(0);
			Integer swId=employeeSchedul.getSwId();
			SchedulDayExample schedulDayExample=new SchedulDayExample();
			schedulDayExample.createCriteria().andSwIdEqualTo(swId)
			.andSdDateEqualTo(attendanceDate);
			List<SchedulDayKey> sdList=schedulDayMapper.selectByExample(schedulDayExample);
			if (sdList.size()>0) {
				isRestDay=0;
			}
		}
		TipsBean tipsBean=new TipsBean();
		tipsBean.setIsRestDay(isRestDay);
		return tipsBean;
	}

}
